###############################################
# Order of the Sacred Magdalene
# Tribal -> Magdalene Feudal Government
#
# File:
# decisions/ggsm_magdalene_feudalization.txt
#
# Adds a distinct Magdalene feudalization decision.
# It does not attempt to replace either vanilla
# Adopt Feudalism decision.
###############################################

decisions = {

	#######################################
	# ADOPT MAGDALENE FEUDALISM
	#######################################

	ggsm_adopt_magdalene_feudalism = {
		is_high_prio = yes
		only_rulers = yes

		potential = {
			is_playable = yes
			is_female = yes
			religion = catholic
			society_member_of = ggsm_order_sacred_magdalene
			government = ggsm_magdalene_tribal_government
		}

		allow = {
			war = no

			# Keep the warning visible in the decision tooltip while
			# hiding the mechanical always-true condition behind it.
			custom_tooltip = {
				text = ggsm_adopt_magdalene_feudalism_warning
				hidden_tooltip = {
					always = yes
				}
			}

			OR = {

				#######################################
				# INDEPENDENT RULER REQUIREMENTS
				#######################################

				AND = {
					independent = yes

					primary_title = {
						has_law = tribal_organization_4
					}

					capital_holding = {
						OR = {
							AND = {
								holding_type = tribal
								has_building = tb_hillfort_4
							}
							holding_type = castle
						}
					}
				}


				#######################################
				# VASSAL RULER REQUIREMENTS
				#######################################

				AND = {
					independent = no

					liege = {
						OR = {
							is_feudal = yes
							is_republic = yes
							is_theocracy = yes
						}
					}
				}
			}
		}

		effect = {

			#######################################
			# PRESERVE MAGDALENE STATE
			#######################################

			if = {
				limit = {
					OR = {
						has_character_flag = ggsm_magdalene_succession_enacted
						any_demesne_title = {
							has_law = succ_magdalene_primogeniture
						}
					}
				}

				set_character_flag = ggsm_feudalization_preserve_magdalene_succession
			}

			# Mark every personally held temple so it can be reclaimed
			# if CK2's government validation temporarily hands it away.
			any_demesne_title = {
				limit = {
					tier = BARON
					holding_type = temple
				}

				set_title_flag = ggsm_feudalization_preserve_temple
			}


			#######################################
			# CONVERT DEMESNE TRIBES TO CASTLES
			# AND BUILD CITY / TEMPLE
			#######################################

			hidden_tooltip = {
				any_demesne_title = {
					limit = {
						tier = BARON
						holding_type = tribal
					}

					convert_to = CASTLE
					refill_holding_levy = yes


					#######################################
					# VANILLA-STYLE COUNTY DEVELOPMENT
					#######################################

					# In every personally held tribal county with at least
					# three total holding slots, ensure that a city and a
					# temple are created when empty slots are available.
					if = {
						limit = {
							location = {
								num_of_max_settlements >= 3
							}
						}


						#######################################
						# BUILD A CITY IF ONE IS MISSING
						#######################################

						if = {
							limit = {
								location = {
									num_of_empty_holdings >= 1
								}

								dejure_liege_title = {
									NOT = {
										any_direct_de_jure_vassal_title = {
											holding_type = city
										}
									}
								}
							}

							location = {
								build_holding = {
									type = city
								}
							}
						}


						#######################################
						# BUILD A TEMPLE IF ONE IS MISSING
						#######################################

						if = {
							limit = {
								location = {
									num_of_empty_holdings >= 1
								}

								dejure_liege_title = {
									NOT = {
										any_direct_de_jure_vassal_title = {
											holding_type = temple
										}
									}
								}
							}

							location = {
								build_holding = {
									type = temple
								}
							}
						}
					}
				}


				#######################################
				# NOTIFY LIEGE IF VASSAL
				#######################################

				if = {
					limit = {
						independent = no
					}

					liege = {
						character_event = {
							id = 55100
						}
					}
				}
			}

			#######################################
			# VANILLA ACHIEVEMENT FLAG
			#######################################

			set_character_flag = achievement_not_a_tribe


			#######################################
			# PLAYER CHRONICLE
			#######################################

			if = {
				limit = {
					ai = no
				}

				chronicle = {
					entry = CHRONICLE_ADOPTED_FEUDALISM
					picture = GFX_evt_castle_construction
				}
			}


			#######################################
			# GOVERNMENT ASSIGNMENT
			#######################################

			# The custom feudal government currently validates with
			# is_feudal = yes. Enter the feudal government group first,
			# then immediately assign the Magdalene government.
			# All state that CK2 can disturb during this validation is
			# restored below before the decision finishes.
			hidden_tooltip = {
				set_government_type = feudal_government
				set_government_type = ggsm_magdalene_feudal_government
			}


			#######################################
			# RESTORE MAGDALENE STATE
			#######################################

			if = {
				limit = {
					government = ggsm_magdalene_feudal_government
				}

				# Reclaim every temple personally held before the switch.
				any_realm_title = {
					limit = {
						tier = BARON
						holding_type = temple
						has_title_flag = ggsm_feudalization_preserve_temple
					}

					if = {
						limit = {
							NOT = { holder = ROOT }
						}

						grant_title = ROOT
					}

					clr_title_flag = ggsm_feudalization_preserve_temple
				}

				# Reapply Magdalene Primogeniture immediately if it was
				# active before the government transition.
				if = {
					limit = {
						has_character_flag = ggsm_feudalization_preserve_magdalene_succession
					}

					any_demesne_title = {
						limit = {
							OR = {
								tier = COUNT
								tier = DUKE
								tier = KING
								tier = EMPEROR
							}
						}

						add_law = {
							law = succ_magdalene_primogeniture
							cooldown = no
							opinion_effect = no
						}
					}

					recalc_succession = yes
					clr_character_flag = ggsm_feudalization_preserve_magdalene_succession
				}
			}


			#######################################
			# FEUDAL ADMINISTRATION
			#######################################

			if = {
				limit = {
					higher_tier_than = king
				}

				primary_title = {
					add_law = feudal_administration
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}
}
